<?php
namespace Home\Controller;
use \Think\Controller;
//新闻内容控制
class ArticleController extends HomeController
{
	
	public function index(){

		$id=I('get.aid/d');
		if($id<=0){
			echo '11111';
			$this->redirect('Index/index');
			// $this->display('Article/index');
			exit;
		}
		// v($id);exit;
		
		$hot = M('category')
		        ->field('a.title, i.img, a.id aid')
		        ->table('hx_article a, hx_image i, hx_user u')
		        ->where('u.id=a.user_id AND i.art_id=a.id AND a.status=1')
		        ->order('a.read_times desc')
		        ->limit(3)
		        ->select();

		$this->assign('hot', $hot);
		$Article = M("article"); // 实例化Article对象
		$Article->where('id=' .$id) ->setInc('read_times',1); // 文章阅读数加1
		$this->assign('title','新闻快询');
		$this->comment();
		$this->article();
		
	}
	//文章和评论的多变联查
	public function article(){
		//遍历文章
		$id=I('get.aid/d');
		if($id<=0){
			$this->display('Article/index');
			exit;
		}

		$article=M('article')
				// ->fetchSql(true)
				->field('a.id aid,a.title,a.create_time,a.zan,a.read_times,a.content,a.status,a.time,a.cover,u.name,u.id uid, c.cname, h.img_150')
				->table('hx_user u,hx_article a,hx_category c, hx_headimg h')
				->where('u.id=a.user_id AND  a.id = '.$id.' AND a.status =1 AND c.id=a.cate_id AND h.id=u.headimg_id')
				->find();
		// dump($article);exit;

		//作者文章数量
		
		// v($id);
		if(empty($article)){
			// echo '13121';
			$this->assign('article',$article);
			$this->display('Article/index');
			exit;
		}else{
			$uid=$article['uid'];
			$ucount=M('article')->where('user_id='.$uid)->count();
			
			// 作者评论条数
			$this->assign('ucount',$ucount);
			// $pd=M('comment')->where('user_id='.$id)->fetchSql(true)->select();
			$pl=M('comment')->where('user_id='.$uid)->fetchSql(true)->select();
			// v($pl);exit;
			if($pl== null){
				 $ucon= 0;
				 $this->assign('ucon',$ucon);
				 $this->assign('article',$article);
				 $this->display('Article/index');
			}else{

			
			$ucon=M('comment')
					->where('user_id='.$uid)
					->count();
					// dump($ucon);
					// echo '11213';
	             $this->assign('article',$article);
	             $this->assign('ucon',$ucon);
	             $this->display('Article/index');
		       }
		}
	}
		//评论结束
		public function comment(){
		// $id =3;
		$id=I('get.aid/d');
		if($id<=0){
			$this->redirect('Article/index');
			exit;
		}
		// v($id);
		$pd=M('comment')->where('art_id='.$id)->select();
		// v($pd);
		
		if($pd == array()){
			$comment=null;
			$this->assign('comment',$comment);
		}else{
			// exit;
		$comment=M('comment')
				// ->fetchSql(true)
				->field('c.c_content,c.com_time,u.name')
				->table('hx_user u,hx_comment c')
				->where('u.id=c.user_id AND  c.art_id = '.$id.' AND c.status =1')
				->order('c.id desc')
				->select();
		// $comment=D('comment')->relation(ture)->find();
		// dump($comment);
		$this->assign('comment',$comment);
		}
	}



	// 评论添加
	public function conadd(){
		$aid=I('post.aid');
		// dump($aid);
		$context=I('post.context','','htmlspecialchars');
		// dump($context);
		$uid=$_SESSION['user']['id'];
		// echo $uid;
		$arr['art_id']=$aid;
		$arr['user_id']=$uid;
		$arr['c_content']=$context;
		$arr['com_time']=time();
		// dump($arr);
		//过滤数据
		$conadd=M('Comment');
		if(!$conadd->create($arr)){
			//如果创建失败 表示验证没有通过 输出错误提示信息
			$this->error($conadd->getError());
		}else{
			//验证通过
			if(D('Comment')->add($arr)>0){
				$this->success('添加成功');
			}else{
				$this-error('添加失败');
			}
		}
	}
	public function zan1(){
		$aid=I('get.aid');
		// v($aid);
		// $this->ajaxReturn($aid);	
		// exit;
		if($_SESSION['zan']<1){
			$_SESSION['zan']=$aid;
			M('article')->where('id=' .$aid) ->setInc('zan',1);
			$add=M('article')->field('zan')->where('id='.$aid)->find();
			$this->ajaxReturn($add['zan']);
		}else{
			$this->ajaxReturn(null);	
		}
		
	}
	
}